/**
 * 防抖
 * @param fn  需要防抖的函数
 * @param delay  防抖时间
 */
export const debounce = function (fn: Function, delay: number) {
  let timer: any = null
  return function () {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(this, arguments)
    }, delay)
  }
}

/**
 * 节流
 * @param fn  需要节流的函数
 * @param delay  节流时间
 */
export const throttle = function (fn: Function, delay: number) {
  let flag = true
  return function () {
    if (!flag) return
    flag = false
    setTimeout(() => {
      fn.apply(this, arguments)
      flag = true
    }, delay)
  }
}
